/**
 * 起重机械设备实时数据craneRecordData.js
 * 版本号： v1.0.0
 */
$(function () {

    /**
     *起重机械实时数据列表
     */
    var lastChecked = 0;
    $("#craneRecordData_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/craneRecordData/craneRecordDataList.do',
        idField: 'prId',
        frozenColumns: [[
            {field: 'prId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'prName', title: '工程项目', width: 200,fixed: true},
                {field: 'unitBuild', title: '建设单位', width: 140,fixed: true,
                    formatter:function(value){
                        if(value){
                            return value.unitName
                        }
                    }
                },
                {field: 'unitOperator', title: '施工单位', width: 110,fixed: true,
                    formatter:function(value){
                        if(value){
                            return value.unitName
                        }
                    }
                },
                {field: 'unitSupervisor', title: '监理单位', width: 140,
                    formatter:function(value){
                        if(value){
                            return value.unitName
                        }
                    }
                }
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#craneRecordData_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#craneRecordData_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#craneRecordData_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#craneRecordData_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

    //查询所属项目绑定
    bindCombo_project("prId_");

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            craneRecordData_doSearch();
                        }
                    });
                }
                //查看设备列表按钮
                if("deviceListBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        width:100,
                        handler: function () {
                            deviceList();
                        }
                    });
                }
                //查看历史数据按钮
                if("historyDataBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        width:100,
                        handler: function () {
                            historyData();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            craneRecordData_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 执行查询
 */
function craneRecordData_doSearch() {

    //刷新dg页码
    refreshPageNumber("craneRecordData_dataList");

    $("#craneRecordData_dataList").datagrid("reload", {
        prId: $("#prId_").combobox("getValue")
    });
    //清楚选中项
    $("#craneRecordData_dataList").datagrid("clearSelections");
}

/**
 * 所属项目bind事件
 * @param projectControl
 */
function bindCombo_project(projectControl) {
    $.ajax({
        url: CONTEXT_PATH + "/project/getProjectListForCombo.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var projectCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < obj.length; i++) {
                projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
            }
            $("#" + projectControl).combobox("loadData", projectCombo);
        }
    })
}

/***
 * 动态刷新页面
 * @param dgId
 */
function refreshPageNumber(dgId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + dgId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + dgId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看项目下相关设备列表
 * @param index
 */
function deviceList(){
    var checked = $("#craneRecordData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#craneRecordData_dataList").datagrid("getSelected");
    var prId = row.prId;
    window.location.href = CONTEXT_PATH + "/craneRecordData/toCraneDeviceList.do?prId=" + prId;
}

/**
 * 查看设备历史数据
 * @param index
 */
function historyData(){
    var checked = $("#craneRecordData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#craneRecordData_dataList").datagrid("getSelected");
    if(row){
        var prId = row.prId;
        window.location.href = CONTEXT_PATH + "/craneRecord/index.do?prId="+prId;
    }
}

/**
 * 查看实时数据详情
 */
function craneRecordData_view(iconCls){
    var row = $("#craneRecordData_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的实时数据详情", timeout: 3000, showType: "slide" });
        return;
    }

    //IFRAM打开窗口
    layoutSub(ssxq.id, ssxq.url + row.prId, iconCls, ssxq.closable, ssxq.title);
}